package com.r.ricervcvcer.search;

import com.alibaba.fastjson.JSON;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class HtmlParseUtil {

    @Autowired
    private RestHighLevelClient client;


    /**
     * 爬取jd数据
     * @throws IOException
     */
    @Test
    public List<Map<String, Object>> getJD(String keyword) throws IOException {
        // jsoup不能抓取ajax的请求，除非自己模拟浏览器进行请求！
        // https://search.jd.com/Search?keyword=java
        String url = "https://search.jd.com/Search?keyword="+keyword;

        // 解析网页
        // Document 就是我们JS的Document对象，你可以看到很多JS语法
            Document document = Jsoup.parse(new URL(url), 3000);
        Element element = document.getElementById("J_goodsList");
//        System.out.println(element);

        // 4、找到所有的li元素
        Elements elements = element.getElementsByTag("li");
        List<Map<String, Object>> list = new ArrayList<>();
        // 获取京东的商品信息
        for (Element el : elements) {
            // 这种网站，一般为了保证效率，一般会延时加载图片
//            String img = el.getElementsByTag("img").eq(0).attr("src");
            String img = el.getElementsByTag("img").eq(0).attr("data-lazy-img");
            String price = el.getElementsByClass("p-price").eq(0).text();
            String title = el.getElementsByClass("p-name").eq(0).text();
            Map map = new HashMap();
            map.put("title", title);
            map.put("price", price);
            map.put("img", img);
            list.add(map);
        }
        return list;
    }

    /**
     * 添加 到elasticsearch
     * @throws IOException
     */
    @Test
    public void addDocument() throws IOException {
         List<Map<String, Object>> java = getJD("vue");

         // 封装到索引库中
        final BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout(TimeValue.timeValueSeconds(2));

        // 循环封装数据
        for (Map<String, Object> obj : java) {
            bulkRequest.add(new IndexRequest("jd_goods")
                    .source(JSON.toJSONString(obj), XContentType.JSON));
        }

        // 发送请求
        final BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);

        if(!bulk.hasFailures()){
            System.out.println("插入成功！");
        }
    }

    /**
     * 关键词查询 带分页处理
     */
    @Test
    public void searchKeyWordElasticSearch() throws IOException {
        // 基本条件的搜索
        SearchRequest searchRequest = new SearchRequest("jd_goods");
        final SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        // 分页
        sourceBuilder.size(5).from(1);

        // 精确匹配 QueryBuilders 根据自己的要求配置查询体条件即可
        final MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("title", "Java");

        // 配置搜索信息
        sourceBuilder.query(matchQueryBuilder);
        sourceBuilder.timeout(TimeValue.timeValueSeconds(60));

        // 搜索
        searchRequest.source(sourceBuilder);
        final SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);

        // 解析结果
        for (SearchHit hit : search.getHits().getHits()) {
            System.out.println(hit.getSourceAsString());
        }
    }

    /**
     * 爬取数据
     * @throws IOException
     */
    @Test
    public void get() throws IOException {
        // jsoup不能抓取ajax的请求，除非自己模拟浏览器进行请求！
        // https://search.jd.com/Search?keyword=java
        String url = "https://search.jd.com/Search?keyword="+"手机";

        // 解析网页
        // Document 就是我们JS的Document对象，你可以看到很多JS语法
        Document document = Jsoup.parse(new URL(url), 3000);
        Element element = document.getElementById("J_selector");
//        System.out.println(element);

        // 4、找到所有的li元素
        Element element1 = element.getElementsByClass("J_valueList v-fixed").get(0);
        Elements li = element1.getElementsByTag("li");

        // 获取京东的商品信息
        for (Element el : li) {
            // 这种网站，一般为了保证效率，一般会延时加载图片
            String img = el.getElementsByTag("img").eq(0).attr("src");
//            String img = el.getElementsByTag("img").eq(0).attr("data-lazy-img");
//            String price = el.getElementsByClass("p-price").eq(0).text();
//            String title = el.getElementsByClass("p-name").eq(0).text();
//            System.out.println("https:"+img);
            if(null != img && ""!=img ){
                System.out.println("https:"+img);
            }

        }
    }

}
